package com.ruoyi.driver.service.impl;

import java.util.List;


import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.driver.domain.TLevel;
import com.ruoyi.driver.mapper.TDriverMapper;
import com.ruoyi.driver.domain.TDriver;
import com.ruoyi.driver.service.ITDriverService;

/**
 * 司机Service业务层处理
 * 
 * @author guguguy
 * @date 2025-03-20
 */
@Service
public class TDriverServiceImpl implements ITDriverService 
{
    @Autowired
    private TDriverMapper tDriverMapper;

    /**
     * 查询司机
     * 
     * @param id 司机主键
     * @return 司机
     */
    @Override
    public TDriver selectTDriverById(Long id)
    {
        return tDriverMapper.selectTDriverById(id);
    }

    /**
     * 查询司机列表
     * 
     * @param tDriver 司机
     * @return 司机
     */
    @Override
    public List<TDriver> selectTDriverList(TDriver tDriver)
    {
        return tDriverMapper.selectTDriverList(tDriver);
    }

    /**
     * 新增司机
     * 
     * @param tDriver 司机
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTDriver(TDriver tDriver)
    {
        tDriver.setCreateTime(DateUtils.getNowDate());
        int rows = tDriverMapper.insertTDriver(tDriver);
        insertTLevel(tDriver);
        return rows;
    }

    /**
     * 修改司机
     * 
     * @param tDriver 司机
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTDriver(TDriver tDriver)
    {
        tDriver.setUpdateTime(DateUtils.getNowDate());
        return tDriverMapper.updateTDriver(tDriver);
    }

    /**
     * 批量删除司机
     * 
     * @param ids 需要删除的司机主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTDriverByIds(Long[] ids)
    {
        tDriverMapper.deleteTLevelByIds(ids);
        return tDriverMapper.deleteTDriverByIds(ids);
    }

    /**
     * 删除司机信息
     * 
     * @param id 司机主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTDriverById(Long id)
    {
        tDriverMapper.deleteTLevelById(id);
        return tDriverMapper.deleteTDriverById(id);
    }

    @Override
    public TLevel selectTLevelById(Long id) {
        return tDriverMapper.selectTLevelById(id);
    }

    /**
     * 新增司机等级信息
     * 
     * @param tDriver 司机对象
     */
    public void insertTLevel(TDriver tDriver)
    {
        List<TLevel> tLevelList = tDriver.getTLevelList();
        Long id = tDriver.getId();
        if (StringUtils.isNotNull(tLevelList))
        {
            List<TLevel> list = new ArrayList<TLevel>();
            for (TLevel tLevel : tLevelList)
            {
                tLevel.setId(id);
                list.add(tLevel);
            }
            if (list.size() > 0)
            {
                tDriverMapper.batchTLevel(list);
            }
        }
    }
}
